package cn.com.dstz.data.post;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public final class IP_PropertiesLoad{

	private static final String PROPERTIES_PATH = "ip.properties";
	
	private static IP_PropertiesLoad load;
	private  Properties ipProperties;
	
	public static IP_PropertiesLoad getInstance( ){
		synchronized( IP_PropertiesLoad.class ){
			if( load == null ){
				load = new IP_PropertiesLoad( );
			}
		}
		return load;
	}
	
	private IP_PropertiesLoad( ){
		try {
			ClassPathResource resource = new ClassPathResource( PROPERTIES_PATH, IP_PropertiesLoad.class );
			ipProperties = PropertiesLoaderUtils.loadProperties(resource);
		}
		catch (IOException ex) {
			throw new IllegalStateException("Could not load 'ip.properties': " + ex.getMessage());
		}
	}
	
	public String [ ] getIps( ){
		String value = ipProperties.getProperty( "ip" );
		if( StringUtils.isBlank( value  )){
			throw new RuntimeException( "无法获取IP列表" );
		}
		
		return StringUtils.split( value, "," );
	}

	public Properties getIpProperties( ) {
		return ipProperties;
	}

	public void setIpProperties( Properties ipProperties ) {
		this.ipProperties = ipProperties;
	}
	
}
